<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>找回密码</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="shortcut icon" th:href="@{../../favicon.ico}" type="image/x-icon"/>
    <link rel="stylesheet" th:href="@{../../css/font.css}">
    <link rel="stylesheet" th:href="@{../../css/xadmin.css}">
    <link rel="stylesheet" th:href="@{../../css/login.css}">
    <script charset="utf-8" th:src="@{../../lib/layui/layui.js}" type="text/javascript"></script>
    <!--    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>-->
    <script th:src="@{../../js/jquery-3.5.1.min.js}" type="text/javascript"></script>

    <style>
        #canvas {
            float: right;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body class="login-bg">

<div class="logo">
    <img alt="西安建筑科技大学总务处" src="" th:src="@{../../images/logo.png}">
</div>

<div class="login">
    <div class="message">找回密码</div>
    <div style="width: 100%; height: 30px"></div>

    <form class="layui-form">
        <div class="layui-form-item">
            <label for="username">用户名</label>
            <input autocomplete="off" class="layui-input" id="username" lay-verify="L_userName" placeholder="请输入用户名"
                   required="required" type="text">
        </div>
        <hr class="hr15">
        <div class="layui-form-item">
            <label for="phone">手机号
                <input autocomplete="off" class="layui-input" id="phone" lay-verify="L_phone" placeholder="请输入预留手机号"
                       required="required" type="text">
            </label>
        </div>
        <hr class="hr15">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label for="code">图形验证码
                        <input autocomplete="off" class="layui-input" id="code" lay-verify="L_code" placeholder="请输入验证码"
                               required="required" type="text">
                    </label>
                </div>
            </div>
            <canvas height="45px" id="canvas" width="125px"></canvas>
        </div>
        <hr class="hr15">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label for="message">手机验证码
                        <input autocomplete="off" class="layui-input" id="message" lay-verify="L_message"
                               placeholder="请输入短信验证码" required="required"
                               type="text">
                    </label>
                </div>
            </div>
            <input autocomplete="off" height="45px" id="getCode" onclick="sendMsg(this)" style="width: 125px"
                   type="button"
                   value="获取验证码">
        </div>
        <hr class="hr15">
        <div class="layui-form-item">
            <input class="layui-btn" lay-filter="next" lay-submit="" type="submit" value="下一步">
        </div>
    </form>
</div>

<div class="copyright">
    <a href="http://zwc.xauat.edu.cn/" style="color: white" target="_blank">版权所有　© 西安建筑科技大学总务处</a>
</div>

<script>
    $(function () {
        var show_num = [];
        draw(show_num);
        var count = 30;

        $("#canvas").on('click', function () {
            draw(show_num);
        });

        $("#code").on('blur', function () {
            var input = $("#code").val().toLowerCase();
            var code = show_num.join("");
            if (input !== code) {
                layer.alert("验证码输入有误，请重新输入");
            }
        });

        $("#getCode").on('click', function () {
            $("#getCode").text(count + "后再获取");
            count--;
        });
    });

    function draw(show_num) {
        var canvas_width = $('#canvas').width();
        var canvas_height = $('#canvas').height();
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width;
        canvas.height = canvas_height;
        var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
        var aCode = sCode.split(",");
        var aLength = aCode.length;//获取到数组的长度

        for (var i = 0; i <= 3; i++) {
            var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            var txt = aCode[j];//得到随机的一个内容
            show_num[i] = txt.toLowerCase();
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";

            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }

    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    function sendMsg() {
        var phone = $("#phone").val();
        var username = $("#username").val();
        $.ajax({
            url: "/admin/sendMsg",
            data: {
                username: username,
                phone: phone
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 1, time: 1500, shade: 0.4});
                    timer();
                } else if (data.code === 1) {
                    document.getElementById("phone").value = "";
                    layer.msg(data.msg, {icon: 2, time: 1500, shade: 0.4});
                } else {
                    layer.msg(data.msg, {icon: 3, time: 1500, shade: 0.4}, function () {
                        window.location.reload();
                    })
                }
            }
        })
    }

    var wait = 60

    function timer() {
        if (wait === 0) {
            $("#getCode").val("获取验证码")
                .removeAttr("disabled")
                .css("background", "#189F92").css("cursor", "pointer");
            wait = 10;
        } else {
            $("#getCode").attr("disabled", "true")
                .css("border-color", "fbfbfb").css("background", "#ccc").css("cursor", "not-allowed")
                .val(wait + "秒后重发");
            wait--;
            setTimeout(function () {
                timer()
            }, 1000);
        }
    }

    layui.use(['form', 'layer', 'jquery'], function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        // 自定义验证
        form.verify({
            L_phone: [/^1[3|4|5|7|8][0-9]{9}$/, '请输入正确的手机号码']
            , L_userName: function (value) {
                if (value.length < 5) {
                    return "用户名至少5个字符";
                }
            }
            , L_message: function (value) {
                if (value.length !== 6) {
                    return "短信验证码为6位数字";
                }
            }
        });

        form.on('submit(next)',
            function () {
                var code = document.getElementById("message").value;
                $.ajax({
                    url: '/admin/verify',
                    data: {
                        code: code
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 1500, shade: 0.4}, function () {
                                window.location.href = "/admin/resetPwd";
                            })
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1500, shade: 0.4}, function () {
                                document.getElementById("message").value = "";
                            })
                        }
                    }
                });
                return false;
            })
    });
</script>
</body>
</html>